<template>
  <Dialog class="InventoryManageDialog" :visible="addVisible" title="" :haderBox="0"  :top="'5vh'" :width="1080" @close="addVisible = false">
    <div style="height: 735px;overflow-y: auto;padding:0px 20px 0px  20px;" v-if="addVisible == true" >
    
      <!-- <TagsSelect :itemWidth="'120px'" style="border-bottom: 1px solid #E7E7E7;" @call-parent="parentMethod" :list="list" :types="types" /> -->
      <el-tabs
          v-model="types" v-if="list.length > 0" 
          class="custom-tabs"
          >
          <el-tab-pane
              v-for="(tab,index) in list"
              :key="index"
              :label="tab.name"
              :name="tab.perms"
              >
          </el-tab-pane>
      </el-tabs> 
      <div style="height:650px;overflow: auto;padding-right: 10px;" v-if="types == 'jbxx'">
         <from1 :dataType="dataTypes" ref="from1Ref"  :dataInfos="dataInfo ?? {}"  @closeDrawer="closeDrawer"></from1>
      </div>
      <StockDrugDetail :storageId="storageIds" :drugId="drugId" v-if="types == 'inventory-mx' && drugId"></StockDrugDetail>
      <StockDrugTrans  :storageId="storageIds" :drugId="drugId" v-if="types == 'inventory-ls' && drugId"></StockDrugTrans>
    </div>
 
    <template #footer v-if="types == 'jbxx'">
      <el-button type="primary" @click="from1RefOpen()">{{dataTypes == 0 ? "保存":'确定'}}</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">

import {getDrugProfileDetail} from '@/api/drug'
import type {Profileadd} from '@/types/drug'
import from1 from './form1.vue'
import StockDrugDetail from './StockDrugDetail.vue'
import StockDrugTrans from './StockDrugTrans.vue'
import stores from '@/store';

const addVisible = ref(false);
const list = ref<any>([{
  name: '基本信息',
  perms: 'jbxx'
},{
  name: '库存明细',
  perms: 'inventory-mx'
},{
  name: '变动流水',
  perms: 'inventory-ls'
}])

const from1Ref = ref<InstanceType<typeof from1>>();
const from1RefOpen = () => {
  from1Ref.value?.open()
}
const route = useRoute();
// let menuList = ref<any>( await stores.useBaseDataStore().findMenuList(route.query.submenuId));
// //console.log(route.query.submenuId);

// let menuItem = ref<any>(menuList.value.filter((item:any) => item.menuType == 2 ));
const drugId = ref<any>('')
const storageIds = ref<string | number>('')
const dataInfo = ref<Profileadd>()
const types = ref<string>()

defineExpose<{
  open: (type:number,storageId:string | number) => void;
  open1: (menu: number,storageId:string | number) => void;
}>({
  open: (type:number,storageId:string | number) => {
    if(type == 0){
      dataTypes.value = 0
    }
    list.value = [
      {
        name: '基本信息',
        perms: 'jbxx'
      }
    ]
    types.value  = 'jbxx'
    dataInfo.value = {
       drugCategory: '',
        genericName: '',
        approvalNumber: '',
        drugIngr: '',
        dosageForm: '',
        mfrId: '',
        drugDosageUnit:'',
        drugDosage: '',  
        minPackQty: '',
        drugName: '',
        minPackUnit: '',
        packUnit: '',
        price: '',
        stockWarningQty: '',
        isMinSplit: '',
        usageList: [
            {
                usageCode: "",
                singleMin: "",
                singleMax: "",
                dailyMin: "",
                dailyMax: "",
                minAgeYear: "",
                minAgeMonth: "",
                maxAgeYear: "",
                maxAgeMonth: "",
            }
        ],
        conflicts: '',
        recoms: '',
        instrs: '',
    }

    storageIds.value  = storageId
    addVisible.value = true;
  },
  open1:async (menu: number,storageId:string | number) => {
    
    list.value = [
      {
        name: '基本信息',
        perms: 'jbxx'
      },{
        name: '库存明细',
        perms: 'inventory-mx'
      },{
        name: '变动流水',
        perms: 'inventory-ls'
      }
    ]
    
    //console.log(list.value);

    types.value  = 'jbxx'
    drugId.value = menu
    dataTypes.value = 1
    storageIds.value = storageId
    await getDrugProfileDetailApi()
    addVisible.value = true;
  },
});


const getDrugProfileDetailApi = async():Promise<void>=>{
  let res = await getDrugProfileDetail({
    drugId: drugId.value,
  })
  if(res.code != '200'){
    //ElMessage.error(res.msg);
    return
  }
  let usageList = []
  if(res.data.usageList){
      for (let index = 0; index < res.data.usageList.length; index++) {
      const element = res.data.usageList[index];
      
      usageList.push(
       {
        dailyMax:element?.dailyMax,
        dailyMin:element?.dailyMin,
        maxAgeMonth:element?.maxAgeMonth,
        maxAgeYear:element?.maxAgeYear,
        minAgeMonth:element?.minAgeMonth,
        minAgeYear:element?.minAgeYear,
        singleMax:element?.singleMax,
        singleMin:element?.singleMin,
        usageCode:element?.usageCode,
       }
      )
    }
    res.data.usageList =usageList
  }
  if(Number(res.data.price) > 0){
    res.data.price = Number((res.data.price / 100).toFixed(2))
  }

  if(Number(res.data.splitPrice) > 0){
    res.data.splitPrice = Number((res.data.splitPrice / 100).toFixed(2))
  }
  dataInfo.value = res.data
}



const dataTypes = ref<number>(0)
const parentMethod = (e: string) => {
  types.value = e
};

const emit = defineEmits(['closeDrawer']);
const closeDrawer = () => {
    //console.log('返回');
    addVisible.value = false;
    emit('closeDrawer');
};
</script>
<style lang="scss" scoped>
  .InventoryManageDialog{
    .el-dialog{
      padding: 0 !important;
    }
    .custom-tabs{
      margin-bottom: 10px;
    }
    ::v-deep(.el-tabs__header) {
        margin: 0;
    }
    ::v-deep(.el-tabs__item) {
      width: 90px !important; /* 设置每个tab的宽度 */
      padding: 0 !important; /* 去除默认padding */
      font-size: 16px !important; /* 设置字体大小 */
    }
    ::v-deep(.el-tabs__nav-wrap:after){
        height: 1px !important;
    }
  }




</style>
